package com.example.zuoye;

/**
 * Created by Administrator on 2022/6/4.
 */

import android.database.Cursor;
import android.nfc.Tag;
import android.util.Log;

import java.io.Console;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DBUtil {

    public Connection Connection_Init()
    {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");//加载驱动
            long in1 = System.currentTimeMillis();
            try {
                connection = DriverManager.getConnection(
                        "jdbc:mysql://10.21.151.218:3306/test?useUnicode=true&characterEncoding=utf8",
                        "root",
                        "root");// 输入连接参数
                System.out.println(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            long end = System.currentTimeMillis();
            System.out.println("连接耗时" + (end - in1));//连接耗时
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public ResultSet getQuestion(Connection conn) {

        ResultSet rs = null;
        try {
            String sql = "select * from question";
            Statement stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rs;

    }
}







 /* while (rs!=null&&rs.next())
                {
                    Question q=new Question();
                    q.setId(rs.getInt("id"));
                    q.setQuestion(rs.getString("question"));
                    q.setAnswerA(rs.getString("answerA"));
                    q.setAnswerB(rs.getString("answerB"));
                    q.setAnswerC(rs.getString("answerC"));
                    q.setAnswerD(rs.getString("answerD"));
                    q.setAnswer(rs.getInt("answer"));
                    q.setExplaination(rs.getString("explaination"));
                    q.setSelectedAnswer(-1);
                    list.add(q);
                }
               /*for (Question q:list)
                {
                    System.out.println(q.toString());
                }                        */

        /*List<Question> questionlist = new ArrayList<>();

        Cursor cursor =db.rawQuery("select * from Question1",null);

        if(cursor.getCount()>0){

            cursor.moveToFirst();//将cursor移动到第一个光标上

            int count=cursor.getCount();
//将cursor中的每一条记录生成一个question对象，并将该question对象添加到list中
            for(int i=0;i<count;i++)
                cursor.moveToPosition(i);

            Question question =new Question();
            question.id=cursor.getInt(cursor.getColumnIndex("id"));
            question.question=cursor.getString(cursor.getColumnIndex("question"));
            question.answerA=cursor.getString(cursor.getColumnIndex("answerA"));
            question.answerB=cursor.getString(cursor.getColumnIndex("answerB"));
            question.answerC=cursor.getString(cursor.getColumnIndex("answerC"));
            question.answerD=cursor.getString(cursor.getColumnIndex("answerD"));
            question.answer=cursor.getInt(cursor.getColumnIndex("answer"));
            question.explaination=cursor.getString(cursor.getColumnIndex("explanation"));
            question.selectedAnswer=-1;//表示没有选择任何选项
            questionlist.add(question);
        }
        return questionlist;
    };
*/

